package com.kgc.kmall.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.Member;
import com.kgc.kmall.bean.MemberExample;
import com.kgc.kmall.bean.MemberReceiveAddress;
import com.kgc.kmall.bean.MemberReceiveAddressExample;
import com.kgc.kmall.service.MemberService;
import com.kgc.kmall.user.mapper.MemberMapper;
import com.kgc.kmall.user.mapper.MemberReceiveAddressMapper;
import com.kgc.kmall.util.RedisUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;


import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class MemberServiceImpl implements MemberService {

    @Resource
    MemberMapper memberMapper;
    @Resource
    RedisUtil redisUtil;

    @Resource
    MemberReceiveAddressMapper memberReceiveAddressMapper;

    @Override
    public List<Member> selectAllMember() {
        List<Member> members = memberMapper.selectByExample(null);
        return members;
    }

    @Override
    public Member login(String userName, String password) {
        //从redis中查询,如果我没有从db查询
        Jedis jedis=null;
        try {
            jedis=redisUtil.getJedis();
            String userInfo = jedis.get("user" + userName + ":info");
            if (StringUtils.isNotBlank(userInfo)){
                Member member = JSON.parseObject(userInfo, Member.class);
                if (member.getPassword().equals(password)){
                    return member;
                }else {
                    return null;
                }
            }else {
            Member member=loginFromDb(userName,password);
                jedis.setex("user:" + userName + ":info", 60 * 60 * 24, String.valueOf(member));
                return member;
            }
        }catch (Exception ex){

        }finally {
            jedis.close();
        }
        return null;
    }

    @Override
    public void addToken(String token, Long id) {
        Jedis jedis = redisUtil.getJedis();
        jedis.setex("user:"+id+":token",60*60*2,token);

        jedis.close();
    }

    @Override
    public Member selectAllBysourceUid(Long uid) {
        MemberExample example=new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andSourceUidEqualTo(uid);
        List<Member> members = memberMapper.selectByExample(example);
        if (members!=null&&members.size()>0){
            return members.get(0);
        }
        return null;
    }

    @Override
    public void add(Member member) {
        memberMapper.insertSelective(member);
    }

    @Override
    public List<MemberReceiveAddress> getReceiveAddressByMemberId(Long memberId) {
        MemberReceiveAddressExample example=new MemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<MemberReceiveAddress> memberReceiveAddresses = memberReceiveAddressMapper.selectByExample(example);
        return memberReceiveAddresses;
    }

    @Override
    public MemberReceiveAddress getReceiveAddressById(long l) {
        return memberReceiveAddressMapper.selectByPrimaryKey(l);
    }


    private Member loginFromDb(String userName, String password) {
        MemberExample example=new MemberExample();
        example.createCriteria().andUsernameEqualTo(userName).andPasswordEqualTo(password);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size()>0&&members!=null){
            return members.get(0);
        }else{
            return null;
        }
    }
}

